class Solution {
public:
    int maxValue(vector<vector<int>>& grid) {
        int sum = 0;
        for (int i = 0; i < grid.front().size(); ++i) {
            sum += grid[0][i];
            grid[0][i] = sum;
        }
        sum = 0;
        for (int i = 0; i < grid.size(); ++i) {
            sum += grid[i][0];
            grid[i][0] = sum;
        }
        for (int i = 1; i < grid.size(); ++i) {
            for (int j = 1; j < grid[i].size(); ++j) {
                grid[i][j] += max(grid[i - 1][j], grid[i][j - 1]);
            }
        }
          return grid.back().back();
    }
};